package simplelab;


//jcanvas


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.Graphics;
import java.awt.Canvas;

import java.awt.Color;
import java.awt.Component;
import java.awt.Point;
import java.util.ArrayList;


public class DebugMap {
	private static World world;
	public static Integer[][] data;
	private static JFrame frame;
	private static Point pos;
	private static int dir;
	private static JTable table;
	public static MyCanvas mcanvas;
	public DebugMap(World w){
		world = w;
		table = new JTable();
		frame = new JFrame("Debug");
        frame.setSize(world.worldHeigth*8 +40,world.worldWidth*8 +40);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		mcanvas= new MyCanvas();
        frame.getContentPane().add(mcanvas);
        frame.setVisible(true);
		
	}
	
	
	public class MyCanvas extends Canvas {
		public MyCanvas() {
		}
	 
		public void paint(Graphics graphics, Point pos,int dir,ArrayList<Food> Foods) {
			data = new Integer[world.worldWidth][world.worldHeigth];
			for(int i = 0; i < world.worldWidth; ++i){
				
				for(int l = 0; l < world.worldHeigth; ++l){
					//data[i][l] = new Integer( world.tile[l][i].value); 
					graphics.setColor(new Color(255-world.tile[i][l].value*50,255-world.tile[i][l].value*50,255-world.tile[i][l].value*50));
					graphics.fillRect(i*8+1, l*8+1, 7, 7);
				} 
				graphics.setColor(Color.black);
				graphics.drawLine(0, i*8, world.worldWidth*8, i*8);	
				graphics.drawLine(i*8, 0, i*8, world.worldHeigth*8);	
			}
			graphics.setColor(Color.black);
			for(int i = 0; i < world.worldWidth; ++i){		
				graphics.drawLine(0, i*8, world.worldWidth*8, i*8);	
			}
                        
                        int plus = 1;
                        if(dir == 0 || dir == 3) plus = -1;
			 		
			//put the seen tiltes to a list
                        graphics.setColor(new Color(25,25,220));
                        int tempX, tempY;
			for(int i = 1; i < 11; ++i){
				for(int l = -1 * i; l <= i; ++l){
					if(dir % 2 == 0){
						tempX = pos.x + l;
						tempY = pos.y + plus * i;
					}else{
						tempX = pos.x + plus * i;
						tempY = pos.y + l;
					}
					if(tempX > -1 && tempX < world.worldWidth && tempY > -1 && tempY < world.worldHeigth )
						graphics.fillRect(tempX*8+1, tempY*8+1, 7, 7);
				}
			}
                        
                        graphics.setColor(Color.red);
                        for(int i = 0; i<Foods.size();i++){
                             graphics.fillRect(Foods.get(i).position.x*8+1, Foods.get(i).position.y*8+1, 7, 7);
                            
                        }
                        graphics.setColor(Color.green);
                        graphics.fillRect(pos.x*8+1, pos.y*8+1, 7, 7);
                        //this.paint(graphics);
			/* We would be using this method only for the sake
			 * of brevity throughout the current section. Note
			 * that the Graphics class has been acquired along
			 * with the method that we overrode. */
		}
 
   
	}
    
    /*public static void createAndShowGUI(){//Point tpos, int tdir) {
        
       
		System.out.println( "1") ;
        JPanel contentPanel = new JPanel();
        String[] columnNames = new String[60];
        data = new Integer[60][60];
        for(int i = 0; i < 60; ++i){
            for(int l = 0; l < 60; ++l){
                data[i][l] = new Integer( world.tile[l][i].value); 
            }    
        }

        
        MyModel model = new MyModel(Color.RED, table.getBackground());
        model.setDataVector(data, columnNames);

        table.setModel(model);
        table.setColumnSelectionAllowed(true);
        table.setDefaultRenderer(Object.class, new TestCellRenderer());
		System.out.println( "2") ;
        
		for(int i = 0; i < 60;i++ ){
            table.getColumnModel().getColumn(i).setPreferredWidth(2);
        }
		
		/*SelectionListener listener = new SelectionListener(table);
        table.getSelectionModel().addListSelectionListener(listener);
        table.getColumnModel().getSelectionModel()
				.addListSelectionListener(listener);*/
		
		//table.setSize(800, 800);
        //JScrollPane scrollPane = new JScrollPane(table);
        /*contentPanel.add(table);
		System.out.println( "3") ;
        //contentPanel.setOpaque(true);
        frame.add(table);

        frame.pack();
        //frame.setLocationByPlatform(true);
		//frame.setSize(800,800);
        frame.setVisible(true);
    }

	//canvas
	
	
    static class TestCellRenderer extends DefaultTableCellRenderer {
        public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus, int row,
                int column) {
            Component c = super.getTableCellRendererComponent(table, value,
                    isSelected, hasFocus, row, column);
            MyModel model = (MyModel) table.getModel();
            Color temp;
            Object cellValue = (table.getValueAt(row, column));
            temp = new Color(255-25*data[row][column].intValue(),255- 25*data[row][column].intValue(), 255-25*data[row][column].intValue());
            c.setBackground(temp);
			c.setSize(5, 5);
            return c;
        }
    }

    static class MyModel extends DefaultTableModel {
        private Object selectedObject;
        private Color selectedColor;
        private Color normalColor;

        public MyModel(Color selectedColor, Color normalColor) {
            this.selectedColor = selectedColor;
            this.normalColor = normalColor;
        }

        public Color getCellColor(int row, int column) {
            if (getValueAt(row, column).equals(selectedObject))
                return selectedColor;
            return normalColor;
        }

        public void setSelectedObject(Object selectedObject) {
            this.selectedObject = selectedObject;
            fireTableRowsUpdated(0, getRowCount());
        }
    }

    static class SelectionListener implements ListSelectionListener {
        private JTable table;

        SelectionListener(JTable table) {
            this.table = table;
        }

        public void valueChanged(ListSelectionEvent e) {
            int rowIndex = table.getSelectedRow();
            int colIndex = table.getSelectedColumn();
            if (!e.getValueIsAdjusting() && colIndex != -1 && rowIndex != -1) {
                ((MyModel) table.getModel()).setSelectedObject(table
                        .getValueAt(rowIndex, colIndex));
            }
        }
    }
*/
   
}